package com.ruoyi.core.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.entity.CoreEntity.Members;
import com.ruoyi.common.entity.CoreEntity.ShippingAddresses;
import com.ruoyi.common.entity.TransactionEntity.Sku;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.core.service.IMembersService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 会员信息Controller
 * 
 * @author 亢哲
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/members")
public class MembersController extends BaseController
{
    @Autowired
    private IMembersService membersService;

    /**
     * 修改会员的手机号
     */
    @PutMapping("/user/phone")
    public AjaxResult updateUserPhone(@RequestBody Map<String, String> request){
        try {
            String phone = request.get("phone");
            Long membersId = SecurityUtils.getUserId();

            int result = membersService.updatePhone(membersId, phone);
            return toAjax(result);
        }catch (IllegalArgumentException e){
            return AjaxResult.error(e.getMessage());
        }catch (Exception e){
            return AjaxResult.error("修改手机号失败");
        }
    }

    /**
     * 修改会员支付密码
     */
    @PutMapping("/user/payPassword")
    public AjaxResult updateUserPayPassword(@RequestBody Map<String, String> request){
        try{
            String payPassword = request.get("payPassword");
            Long membersId = SecurityUtils.getUserId();

            int result = membersService.updatePayPassword(membersId, payPassword);
            return toAjax(result);
        }catch (IllegalArgumentException e){
            return AjaxResult.error(e.getMessage());
        }catch (Exception e){
            return AjaxResult.error("修改支付密码失败");
        }
    }





    /**
     * 通过会员ID获取会员信息及积分变动、签到记录
     */
    @GetMapping("/{userId}/details")
    public AjaxResult getMemberDetails(@PathVariable Long userId) {
        Members member = membersService.getMemberWithDetailsById(userId);
        if (member == null) {
            return AjaxResult.error("未找到该会员信息");
        }
        return AjaxResult.success(member);
    }


    /**
     * 查询会员收藏的商品
     */
    @GetMapping("/sku/{membersId}")  // 去掉 {membersId}
    public AjaxResult getSkuListByMemberId(@PathVariable Long membersId) {
        List<Sku> list = membersService.selectSkuListByMembersId(membersId);
        return AjaxResult.success(list);
    }
    /**
     * 查询会员的收货地址
     */
    @GetMapping("/shippingAddress/{membersId}")  // 去掉 {membersId}
    public AjaxResult getShippingAddressesByMemberId(@PathVariable Long membersId) {
        List<ShippingAddresses> list = membersService.selectShippingAddressesByMembersId(membersId);
        return AjaxResult.success(list);
    }

    /**
     * 查询会员信息列表
     */
    @RequiresPermissions("core:members:list")
    @GetMapping("/list")
    public TableDataInfo list(Members members)
    {
        startPage();
        List<Members> list = membersService.selectMembersList(members);
        return getDataTable(list);
    }

    /**
     * 导出会员信息列表
     */
    @RequiresPermissions("core:members:export")
    @Log(title = "会员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Members members)
    {
        List<Members> list = membersService.selectMembersList(members);
        ExcelUtil<Members> util = new ExcelUtil<Members>(Members.class);
        util.exportExcel(response, list, "会员信息数据");
    }

    /**
     * 获取会员信息详细信息
     */
    @RequiresPermissions("core:members:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(membersService.selectMembersById(id));
    }

    /**
     * 新增会员信息
     */
    @RequiresPermissions("core:members:add")
    @Log(title = "会员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Members members)
    {
        return toAjax(membersService.insertMembers(members));
    }

    /**
     * 修改会员信息
     */
    @RequiresPermissions("core:members:edit")
    @Log(title = "会员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Members members)
    {
        return toAjax(membersService.updateMembers(members));
    }

    /**
     * 删除会员信息
     */
    @RequiresPermissions("core:members:remove")
    @Log(title = "会员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(membersService.deleteMembersByIds(ids));
    }
}
